import avatar_24x24 from '@/assets/avatar/avatar_24x24.png'
import avatar_32x32 from '@/assets/avatar/avatar_32x32.png'
import avatar_64x64 from '@/assets/avatar/avatar_64x64.png'
import avatar_128x128 from '@/assets/avatar/avatar_128x128.png'
import meeting_room_24x24 from '@/assets/avatar/meeting_room_24x24.png'
import meeting_room_32x32 from '@/assets/avatar/meeting_room_32x32.png'
import meeting_room_64x64 from '@/assets/avatar/meeting_room_64x64.png'
import meeting_room_128x128 from '@/assets/avatar/meeting_room_128x128.png'
import { Dept } from '@/pages/index/ManageList/interfaces/meeting'
export const Utils = {
	onErrorBySize: function (accountType: number, size: number) {
		return this.getDefaultAvatarURL(accountType, size)
	},
	removeLastChar: function (str: string) {
		if (!str) {
			return str
		} else if (str.length < 2) {
			return ''
		} else {
			return str.substring(0, str.length - 1)
		}
	},
	getDefaultAvatarURL: function (accountType: number, size: number) {
		if (accountType !== 2) {
			//非会议室
			return this.getUserDefaultAvatarURLBySize(size)
		} else {
			return this.getMeetingRoomDefaultAvatarURLBySize(size)
		}
	},
	getUserDefaultAvatarURLBySize: function (size: number) {
		switch (size) {
			case 24:
				return avatar_24x24
			case 32:
				return avatar_32x32
			case 64:
				return avatar_64x64
			case 128:
				return avatar_128x128
			default:
				return avatar_32x32
		}
	},
	getMeetingRoomDefaultAvatarURLBySize: function (size: number) {
		switch (size) {
			case 24:
				return meeting_room_24x24
			case 32:
				return meeting_room_32x32
			case 64:
				return meeting_room_64x64
			case 128:
				return meeting_room_128x128
			default:
				return meeting_room_32x32
		}
	},
	getDeptName: function (depts: Dept[]) {
		var dept = {
			title: '',
			name: '',
		}
		var arr1 = [],
			arr2 = []
		for (var i = 0; i < depts.length; i++) {
			arr1.push(depts[i].departmentName)
			arr2.push(depts[i].fullPath)
		}
		dept.title = arr2.join('，')
		dept.name = arr1.join('，')
		return dept
	},
	getJobObj: function (depts: Dept[]) {
		var jobObj = {
			title: '',
			name: '',
		}
		var job = ''
		for (var i = 0; i < depts.length; i++) {
			var departmentJob = depts[i].deptPosition
			if (departmentJob) {
				var jobArr = departmentJob.split(',')
				for (var j = 0; j < jobArr.length; j++) {
					var jobSim = jobArr[j].trim()
					var jobAndReg = jobSim + '，'
					if (jobSim && jobSim !== '' && job.indexOf(jobAndReg) === -1) {
						job += jobAndReg
					}
				}
			}
		}
		if (job !== '' && job.lastIndexOf('，') === job.length - 1) {
			job = job.substring(0, job.length - 1)
		}
		jobObj.title = job
		jobObj.name = job
		return jobObj
	},
	code: {
		encodeHTML: function (str: string) {
			var s = ''
			if (str && str.length === 0) {
				return ''
			}
			s = str
			s = s.replace(/&/g, '&amp;')
			s = s.replace(/</g, '&lt;')
			s = s.replace(/>/g, '&gt;')
			s = s.replace(/ /g, '&nbsp;')
			s = s.replace(/'/g, '&#39;')
			s = s.replace(/"/g, '&quot;')
			s = s.replace(/\n/g, '<br>')
			return s
		},
		decodeHTML: function (str: string) {
			var s = ''
			if (str && str.length === 0) {
				return ''
			}
			s = str
			s = s.replace(/&amp;/g, '&')
			s = s.replace(/&lt;/g, '<')
			s = s.replace(/&gt;/g, '>')
			s = s.replace(/&nbsp;/g, ' ')
			s = s.replace(/&#39;/g, "'")
			s = s.replace(/&quot;/g, '"')
			s = s.replace(/<br>/g, '\n')
			return s
		},
	},
	shorten: function (text: string, maxLength: number) {
		var ret = text
		if (this.getBytesLength(ret) > maxLength) {
			var i = 1
			ret = text.substr(0, maxLength / 2 - 3) + '...'
			while (this.getBytesLength(ret) < maxLength) {
				ret = text.substr(0, maxLength / 2 - 3 + i) + '...'
				i++
			}
		}
		return ret
	},
	getBytesLength: function (str: string) {
		// 在GBK编码里，除了ASCII字符，其它都占两个字符宽
		return str.replace(/[^\x00-\xff]/g, 'xx').length
	},
}
